﻿using Infrastructure.WebApi;
using Microsoft.AspNetCore.Mvc;
using Neoit.Utils.Extensions;
using Neoit.Utils.Tools;
using Web.Middlewares;

namespace Web.Controllers
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class JobController : ControllerBase
    {
        public IJobService _service { get; set; }
        public JobController(IJobService service)
        {
            _service = service;
        }

        #region 任务
        [HttpGet]
        public async Task<Page<JobResponse>> Get([FromQuery] JobRequest request)
        {
            return await _service.Get(request);
        }
        [HttpPost]
        public async Task Add([FromBody] JobFormRequest request)
        {
            await _service.Add(request);
        }
        [HttpPost]
        public async Task Copy([FromBody] IdRequest request)
        {
            await _service.Copy(request);
        }
        [HttpPut]
        public async Task Update([FromBody] JobFormRequest request)
        {
            await _service.Update(request);
        }
        [HttpDelete]
        public async Task Delete([FromQuery] IdRequest request)
        {
            await _service.Delete(request);
        }
        #endregion

        [HttpPost]
        public async Task Once([FromBody] RunOnceRequest request)
        {
            await _service.Once(request);
        }
        [HttpPut]
        public async Task Enable([FromBody] IdRequest request)
        {
            await _service.Enable(request);
        }
        [HttpPut]
        public async Task Disable([FromBody] IdRequest request)
        {
            await _service.Disable(request);
        }
        [HttpGet]
        public async Task<Page<JobTaskRecord>> GetRecords([FromQuery] JobRecordsRequest request)
        {
            return await _service.GetRecords(request);
        }
        [HttpGet]
        public async Task<List<NoticeDetails>> NoticeDetails([FromQuery] IdRequest request)
        {
            return await _service.NoticeDetails(request);
        }

        [HttpGet, AnonymousRole]
        public async Task<List<string>> GetGroups()
        {
            return await _service.GetGroups();
        }
        [HttpGet, AnonymousRole]
        public string ParseCron([FromQuery] KeyRequest request)
        {
            try
            {
                if (request.Key.Has())
                {
                    return request.Key.ParseCronExpression() + "执行一次";
                }
            }
            catch (Exception)
            {
                return "cron表达式有误！";
            }
            return "";
        }
    }
}